package com.chaokang.service;

import com.chaokang.dao.CommonUserEntityMapper;
import com.chaokang.dao.entity.CommonUserEntity;
import com.chaokang.util.SendSmsUtil;
import com.chaokang.util.ValidateCode;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class CommonUserService {
    @Autowired
    CommonUserEntityMapper commonUserEntityMapper;
    public CommonUserEntity getCommonUserByID(int id){
        return commonUserEntityMapper.getUserById(id);
    }
    public CommonUserEntity getCommonUserByIphone(String iphone){
        return commonUserEntityMapper.getUserByIphone(iphone);
    }
    public int insertCommonUser(CommonUserEntity commonUserEntity){
        if(commonUserEntityMapper.getUserByUsername(commonUserEntity.getUsername())==null&&
                commonUserEntityMapper.getUserByIphone(commonUserEntity.getIphone())==null){
            return commonUserEntityMapper.insertCommonUser(commonUserEntity);

        }
        return 0;


    }
    public CommonUserEntity getCommonUserEntity(CommonUserEntity commonUserEntity){
        if(commonUserEntityMapper.getUserByUsername(commonUserEntity.getUsername())!=null){
            return commonUserEntityMapper.getUserByUsername(commonUserEntity.getUsername());
        }
        if (commonUserEntityMapper.getUserByIphone(commonUserEntity.getIphone())!=null){
            return commonUserEntityMapper.getUserByIphone(commonUserEntity.getIphone());
        }
        return null;
    }
    public int updateVerifyCode(CommonUserEntity commonUserEntity){
        if(getCommonUserByIphone(commonUserEntity.getIphone())==null){
            return commonUserEntityMapper.insertCommonUser(commonUserEntity);

        }
        else {
            return commonUserEntityMapper.updateVerifyCode(commonUserEntity);
        }
    }
    public SendStatus[] sendVerifyCode(CommonUserEntity commonUserEntity){
        Integer vode = ValidateCode.generateValidateCode(4);
        commonUserEntity.setVerifyCode(vode.toString());
        if(updateVerifyCode(commonUserEntity)==0){
            insertCommonUser(commonUserEntity);
        }
        String[] phones = {"+86"+commonUserEntity.getIphone()};
        return SendSmsUtil.sendSms(phones,vode);
    }
    public CommonUserEntity logIn(CommonUserEntity commonUserEntity){

        CommonUserEntity commonUserEntity1 =new CommonUserEntity();
        if(commonUserEntity.getUsername()!=null ){

            commonUserEntity1 =commonUserEntityMapper.getUserByUsername(commonUserEntity.getUsername());

        }
        else if(commonUserEntity.getUsername()==null&&commonUserEntity.getIphone()!=null){
            commonUserEntity1 = commonUserEntityMapper.getUserByIphone(commonUserEntity.getIphone());

        }
        else
            return null;


        if(commonUserEntity1.getPassword().equals(commonUserEntity.getPassword())){
            return commonUserEntity1;
        }
        return null;
    }
    public boolean logInByVarifyCode(CommonUserEntity commonUserEntity){
        CommonUserEntity commonUserEntity1 = getCommonUserEntity(commonUserEntity);
        if(commonUserEntity1.getVerifyCode().equals(commonUserEntity.getVerifyCode())){
            return true;
        }
        return false;
    }
    public CommonUserEntity confimVarifyCode(CommonUserEntity commonUserEntity){
        CommonUserEntity commonUserEntity1 = getCommonUserEntity(commonUserEntity);
        if(commonUserEntity1.getVerifyCode().equals(commonUserEntity.getVerifyCode()))
        {
            return commonUserEntity1;
        }
        return null;
    }
    public CommonUserEntity retrievePassword(CommonUserEntity commonUserEntity){
        CommonUserEntity commonUserEntity1 = getCommonUserEntity(commonUserEntity);
        commonUserEntity1.setPassword(commonUserEntity.getPassword());
        if(commonUserEntityMapper.updatePassword(commonUserEntity1)==1){
            return commonUserEntity1;
        };
        return null;


    }
    public Integer registerCommonUser(CommonUserEntity commonUserEntity){
        CommonUserEntity commonUserEntity1 = commonUserEntityMapper.getUserByIphone(commonUserEntity.getIphone());

        if(commonUserEntity1.getUsername()!= null||commonUserEntityMapper.getUserByUsername(commonUserEntity.getUsername())!=null){

            return 0;
        }

        if(commonUserEntity.getVerifyCode().equals(commonUserEntity1.getVerifyCode())){
            return commonUserEntityMapper.updateCommonUser(commonUserEntity);

        }

        return 0;
    }

}
